import pandas as pd
import numpy as np

def analyse_data(file_name):
    # 读取数据
    df = pd.read_excel(file_name)

    # 处理缺失值
    df = df.copy(deep=True)
    df.fillna(np.nan,inplace=True)  # 填充空值（必须用 inplace=True 或重新赋值）
    df.dropna(axis=1, how='all', inplace=True)  # 删除全为空的列
    df.dropna(axis=0, how='any', inplace=True)  # 删除包含空值的行

    # 特征列和目标列
    feature_name = ['科技人员占企业职工总数比例', '研发费用占销售收入总额比例', '研发费用占成本费用支出总额比例',
                    '净资产（万元）',
                    '成本费用总额（万元）', '销售收入（万元）', '研发费用总额（万元）', '纳税总额（万元）', '企业职工总数',
                    '企业科技人员总数', '总知识产权数']
    target_name = ['资产总额增长率', '主营业务收入增长率', '利润总额增长率']

    # 逐步筛选数据（确保每次筛选都基于前一次的结果）
    # 1. 筛选 "研发费用占销售收入总额比例" 的 99% 分位数以下
    threshold1 = df[feature_name[1]].quantile(0.99)
    df_filtered = df[df[feature_name[1]] < threshold1]

    # 2. 筛选 "研发费用占成本费用支出总额比例" 的 99% 分位数以下
    threshold2 = df_filtered[feature_name[2]].quantile(0.99)
    df_filtered = df_filtered[df_filtered[feature_name[2]] < threshold2]

    # 3. 筛选 "净资产（万元）" 的 1% 分位数以上
    threshold3 = df_filtered[feature_name[3]].quantile(0.01)
    df_filtered = df_filtered[df_filtered[feature_name[3]] > threshold3]

    # 4. 筛选 "研发费用总额（万元）" 的 99% 分位数以下
    threshold6 = df_filtered[feature_name[6]].quantile(0.99)
    df_filtered = df_filtered[df_filtered[feature_name[6]] < threshold6]

    # 5. 筛选 "纳税总额（万元）" 的 99% 分位数以下
    threshold7 = df_filtered[feature_name[7]].quantile(0.99)
    df_filtered = df_filtered[df_filtered[feature_name[7]] < threshold7]

    # 6. 筛选 "企业科技人员总数" 的 99% 分位数以下
    threshold9 = df_filtered[feature_name[9]].quantile(0.99)
    df_filtered = df_filtered[df_filtered[feature_name[9]] < threshold9]

    # 7. 筛选 "总知识产权数" 的 99% 分位数以下
    threshold10 = df_filtered[feature_name[10]].quantile(0.99)
    df_filtered = df_filtered[df_filtered[feature_name[10]] < threshold10]

    # 返回处理后的数据（可选）
    return df_filtered
